package com.pig4cloud.pigx.wallet.websocket.subscribe;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pig4cloud.pigx.common.customize.utils.CommonUtil;
import com.pig4cloud.pigx.common.websocket.holder.SessionKeyGenerator;
import com.pig4cloud.pigx.wallet.entity.CapitalAccount;
import com.pig4cloud.pigx.wallet.service.CapitalAccountService;
import com.pig4cloud.pigx.wallet.vo.AbstractTopicVO;
import com.pig4cloud.pigx.wallet.websocket.AssetListVo;
import com.pig4cloud.pigx.wallet.websocket.CapitalAccountVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 */
@Slf4j
@Component
public class CapitalAccountHandler extends DataListAbstractTopic {

	private final CapitalAccountService capitalAccountService;

	private final SessionKeyGenerator sessionKeyGenerator;

	protected String TOPICNAME = "capitalAccount";

	public CapitalAccountHandler(CapitalAccountService capitalAccountService, SessionKeyGenerator sessionKeyGenerator, CommonUtil commonUtil) {
		this.capitalAccountService = capitalAccountService;
		this.sessionKeyGenerator = sessionKeyGenerator;
		init();
	}

	private void init() {
		wsPush();
	}

	private void wsPush() {
		ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
		service.scheduleAtFixedRate(() -> {
			try {
				for (Map.Entry<WebSocketSession, AbstractTopicVO> entry : subList.entrySet()) {

					if (entry.getKey().isOpen() == false) {
						continue;
					}
					try {
						Object userId = sessionKeyGenerator.sessionKey(entry.getKey());
						CapitalAccountVo capitalAccountVo = new CapitalAccountVo();
						AssetListVo assetListVo = new AssetListVo();
						List<AssetListVo> list = new ArrayList<>();

						CapitalAccount capitalAccount = capitalAccountService.getOne(new LambdaQueryWrapper<CapitalAccount>()
								.eq(CapitalAccount::getUserId, userId)
						);

						assetListVo.setCurrency("1");
						// 精度控制 4 位
						assetListVo.setAvailable(capitalAccount.getAvailable().setScale(4, RoundingMode.DOWN).toPlainString());
						assetListVo.setFrozen(capitalAccount.getFrozen().setScale(4, RoundingMode.DOWN).toPlainString());
						BigDecimal total = capitalAccount.getAvailable().add(capitalAccount.getFrozen()).setScale(4, RoundingMode.DOWN);
						assetListVo.setTotal(total.toPlainString());
						list.add(assetListVo);

						capitalAccountVo.setAssetList(list);
						capitalAccountVo.setTotalAssets(assetListVo.getTotal());
						// 发送消息
						entry.getKey().sendMessage(getTextMessage(entry.getValue().getDataList(), capitalAccountVo));
					} catch (Exception ex) {
						ex.printStackTrace();
						log.info("push last price err:" + ex.getMessage());
					}

				}
			} catch (Exception e) {
				e.printStackTrace();
				log.info("push ranking err:" + e.getMessage());
			}

		}, 0, 300, TimeUnit.MILLISECONDS);
	}


}
